package com.mdlib.droid.util.core;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.mdlib.droid.R;
import com.mdlib.droid.util.GlideCircleTansformV2;
import com.mdlib.droid.util.GlideRoundTransform;


/**
 * Created by librabin on 16/12/24.
 */

public class ImageLoader {

    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final int IMAGE_LOADING = R.mipmap.ic_launcher;
    public static final int IMAGE_ERROR = R.mipmap.ic_launcher;

    // 将资源ID转为Uri
    public static Uri resourceIdToUri(Context mContext, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + "/" + resourceId);
    }
    // 加载网络图片


    // 加载drawable图片
    public static void displayByRes(Context mContext, int resId, ImageView imageView) {
        Glide.with(mContext)
                .load(resId)
                .crossFade()
                .fitCenter()
                .into(imageView);
    }

    public static void displayByRes(Context mContext, ImageView imageView, String type) {
        int resId = 0;

        Glide.with(mContext)
                .load(resId)
                .crossFade()
                .into(imageView);
    }

    /**
     * 圆形
     *
     * @param mContext
     * @param resId
     * @param imageView
     */
    public static void displayByCircleRes(Context mContext, String resId, ImageView imageView) {
        Glide.with(mContext)
                .load(resId)
                .transform(new GlideCircleTansformV2(mContext))
                .crossFade()
                .into(imageView);
    }


    // 加载本地图片
    public static void displayByLocal(Context mContext, String path, ImageView imageView) {
        Glide.with(mContext)
                .load("file://" + path)
                .placeholder(IMAGE_LOADING)
                .error(IMAGE_ERROR)
                .crossFade()
                .into(imageView);
    }

    //
    public static void displayByUrl(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .signature(new StringSignature(String.valueOf(TimeUtils.getNowMills())))
                .into(imageView);
    }

    //
    public static void displayByDefaultUrl(Context context, String url, ImageView imageView) {

        Glide.with(context)
                .load(url)
                .signature(new StringSignature(String.valueOf(TimeUtils.getNowMills())))
                .into(imageView);
    }


    public static void disPlayCircleByUrl(Context context, String url, ImageView imageView,
                                          int placeHolder) {
        Glide.with(context)
                .load(url)
                .bitmapTransform(new GlideCircleTansformV2(context))
                .crossFade()
                .signature(new StringSignature(String.valueOf(TimeUtils.getNowMills())))
                .into(imageView);

    }

    public static void disPlayRoundByUrl(Context context, String url, ImageView imageView) {

        Glide.with(context)
                .load(url)
                .transform(new GlideRoundTransform(context, 4))
                .into(imageView);

    }


}
